#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#define BUFFER_SIZE 64
int main(int argc, char *argv[])/*命令行参数 char*字符串数组*/
{
#if 0
    /*命令行参数的个数*/
    printf("argc = %d\n",argc);
    for(int idx =0;idx<argc;idx++)
    {
        printf("argv[%d] = %s\n",idx,argv[idx]);
    }
    return 0;
#endif
    if(argc !=3)
    {
        printf("param num is invalid.\n");
        exit(-1);
    }
    const char * srcFile = argv[1];
    const char * dstFile = argv[2];
    int srcFd = open(srcFile,O_RDONLY);
    if(srcFd == -1)
    {
        perror("open srcFile error:\n");
        exit(-1);
    }
    /*程序走到这个地方，源文件存在*/
   int dstFd = open(dstFile,O_WRONLY|O_CREAT,0644);
   if(dstFd == -1)
   {
        printf("open dstFile error:\n");
        exit(-1);
   }
   char buffer[BUFFER_SIZE] = {0};
    int readBytes = 0;
    while(1)
    {
        /*读到的字节数*/
        readBytes= read(srcFd,buffer,BUFFER_SIZE -1);
        if((readBytes < BUFFER_SIZE - 1) || (readBytes == 0))
        {
            write(dstFd,buffer,readBytes);
            break;
        }
        write(dstFd,buffer,readBytes);
        /*清除脏数据*/
        memset(buffer,0,sizeof(buffer));
    }
    close(srcFd);
    close(dstFd);
    return 0;
}